package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 分页评论列表查询
     *    参数
     *      movementId
     *      page，
     *      pagesize
     */
    @GetMapping
    public ResponseEntity comments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            String movementId
    ) {
        PageResult result = commentsService.findComments(page,pagesize,movementId);
        return ResponseEntity.ok(result);
    }

    /**
     * 发布评论
     */
    @PostMapping
    public ResponseEntity saveCommment(@RequestBody Map map) {
        //提取参数
        String movementId = (String) map.get("movementId"); //动态id
        String comment = (String) map.get("comment"); //评论内容
        //调用service
        commentsService.saveComment(movementId,comment);
        return ResponseEntity.ok(null);
    }
}
